<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :inline="true" :model="queryParams" label-width="68px">
          <el-form-item label="保养单号" prop="upkeepCode">
            <el-input v-model="queryParams.upkeepCode" clearable placeholder="请输入保养单号" style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="电梯名称" prop="elevatorName">
            <el-input v-model="queryParams.elevatorName" clearable placeholder="请输入电梯名称" style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['iot:upkeepInfo:remove']" :disabled="multiple" icon="Delete" plain type="danger" @click="handleDelete()"
              >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['iot:upkeepInfo:export']" icon="Download" plain type="warning" @click="handleExport">导出 </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="upkeepInfoList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" label="电梯名称" prop="elevatorName" />
        <el-table-column align="center" label="电梯识别码" prop="identifyCode" />
        <el-table-column align="center" label="使用单位" prop="useUnit" />
        <el-table-column align="center" label="维保单位" prop="maintenanceUnit" />
        <el-table-column align="center" label="保养类型" prop="upkeepType">
          <template #default="scope">
            <dict-tag :options="iot_upkeep_type" :value="scope.row.upkeepType" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="保障状态" prop="upkeepState">
          <template #default="scope">
            <dict-tag :options="iot_upkeep_state" :value="scope.row.upkeepType" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="维保人" prop="applicant" />
        <el-table-column align="center" label="截止时间" prop="overTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.overTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="开始时间" prop="startTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="保养时长" prop="upkeepDate" />
        <el-table-column align="center" label="备注" prop="remark" />
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template #default="scope">
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['iot:upkeepInfo:remove']" icon="Delete" link type="primary" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script lang="ts" name="UpkeepInfo" setup>
import { addUpkeepInfo, delUpkeepInfo, getUpkeepInfo, listUpkeepInfo, updateUpkeepInfo } from "@/api/iot/upkeepInfo";
import { UpkeepInfoForm, UpkeepInfoQuery, UpkeepInfoVO } from "@/api/iot/upkeepInfo/types";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { iot_upkeep_state, iot_upkeep_type } = toRefs<any>(proxy?.useDict("iot_upkeep_state", "iot_upkeep_type"));
const upkeepInfoList = ref<UpkeepInfoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const upkeepInfoFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ""
});

const initFormData: UpkeepInfoForm = {
  upkeepId: undefined,
  elevatorId: undefined,
  templateId: undefined,
  upkeepCode: undefined,
  elevatorName: undefined,
  identifyCode: undefined,
  useUnit: undefined,
  maintenanceUnit: undefined,
  upkeepType: undefined,
  upkeepState: undefined,
  applicant: undefined,
  overTime: undefined,
  startTime: undefined,
  upkeepDate: undefined,
  longitude: undefined,
  latitude: undefined,
  roomImg: undefined,
  roomRemark: undefined,
  floorImg: undefined,
  floorRemark: undefined,
  capsulesImg: undefined,
  capsulesRemark: undefined,
  shaftImg: undefined,
  shaftRemark: undefined,
  referUserSign: undefined,
  auditUserSign: undefined,
  referUser: undefined,
  referTime: undefined,
  auditUser: undefined,
  auditTime: undefined,
  avatar: undefined,
  remark: undefined
};
const data = reactive<PageData<UpkeepInfoForm, UpkeepInfoQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    elevatorId: undefined,
    templateId: undefined,
    upkeepCode: undefined,
    elevatorName: undefined,
    identifyCode: undefined,
    useUnit: undefined,
    maintenanceUnit: undefined,
    upkeepType: undefined,
    upkeepState: undefined,
    applicant: undefined,
    overTime: undefined,
    startTime: undefined,
    upkeepDate: undefined,
    longitude: undefined,
    latitude: undefined,
    roomImg: undefined,
    roomRemark: undefined,
    floorImg: undefined,
    floorRemark: undefined,
    capsulesImg: undefined,
    capsulesRemark: undefined,
    shaftImg: undefined,
    shaftRemark: undefined,
    referUserSign: undefined,
    auditUserSign: undefined,
    referUser: undefined,
    referTime: undefined,
    auditUser: undefined,
    auditTime: undefined,
    avatar: undefined,
    params: {}
  },
  rules: {
    upkeepId: [
      { required: true, message: "保养ID不能为空", trigger: "blur" }
    ],
    elevatorId: [
      { required: true, message: "关联电梯ID不能为空", trigger: "blur" }
    ],
    templateId: [
      { required: true, message: "关联模板ID不能为空", trigger: "blur" }
    ],
    upkeepCode: [
      { required: true, message: "保养单号不能为空", trigger: "blur" }
    ],
    elevatorName: [
      { required: true, message: "电梯名称不能为空", trigger: "blur" }
    ],
    identifyCode: [
      { required: true, message: "电梯识别码不能为空", trigger: "blur" }
    ],
    useUnit: [
      { required: true, message: "使用单位不能为空", trigger: "blur" }
    ],
    maintenanceUnit: [
      { required: true, message: "维保单位不能为空", trigger: "blur" }
    ],
    upkeepType: [
      { required: true, message: "保养类型不能为空", trigger: "change" }
    ],
    upkeepState: [
      { required: true, message: "保障状态", trigger: "blur" }
    ],
    applicant: [
      { required: true, message: "维保人不能为空", trigger: "blur" }
    ],
    overTime: [
      { required: true, message: "截止时间不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
    upkeepDate: [
      { required: true, message: "保养时长不能为空", trigger: "blur" }
    ],
    longitude: [
      { required: true, message: "保养位置经度不能为空", trigger: "blur" }
    ],
    latitude: [
      { required: true, message: "保养位置纬度不能为空", trigger: "blur" }
    ],
    roomImg: [
      { required: true, message: "机房图片不能为空", trigger: "blur" }
    ],
    roomRemark: [
      { required: true, message: "机房备注不能为空", trigger: "blur" }
    ],
    floorImg: [
      { required: true, message: "层站图片不能为空", trigger: "blur" }
    ],
    floorRemark: [
      { required: true, message: "层站备注不能为空", trigger: "blur" }
    ],
    capsulesImg: [
      { required: true, message: "轿厢图片不能为空", trigger: "blur" }
    ],
    capsulesRemark: [
      { required: true, message: "轿厢备注不能为空", trigger: "blur" }
    ],
    shaftImg: [
      { required: true, message: "井道图片不能为空", trigger: "blur" }
    ],
    shaftRemark: [
      { required: true, message: "井道备注不能为空", trigger: "blur" }
    ],
    referUserSign: [
      { required: true, message: "提交人签名不能为空", trigger: "blur" }
    ],
    auditUserSign: [
      { required: true, message: "审核人签名不能为空", trigger: "blur" }
    ],
    referUser: [
      { required: true, message: "提交人姓名不能为空", trigger: "blur" }
    ],
    referTime: [
      { required: true, message: "提交时间不能为空", trigger: "blur" }
    ],
    auditUser: [
      { required: true, message: "审核人姓名不能为空", trigger: "blur" }
    ],
    auditTime: [
      { required: true, message: "审核时间不能为空", trigger: "blur" }
    ],
    avatar: [
      { required: true, message: "提交人头像不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询电梯保养信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listUpkeepInfo(queryParams.value);
  upkeepInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  upkeepInfoFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: UpkeepInfoVO[]) => {
  ids.value = selection.map(item => item.upkeepId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加电梯保养信息";
};

/** 修改按钮操作 */
const handleUpdate = async (row?: UpkeepInfoVO) => {
  reset();
  const _upkeepId = row?.upkeepId || ids.value[0];
  const res = await getUpkeepInfo(_upkeepId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改电梯保养信息";
};

/** 提交按钮 */
const submitForm = () => {
  upkeepInfoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.upkeepId) {
        await updateUpkeepInfo(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addUpkeepInfo(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: UpkeepInfoVO) => {
  const _upkeepIds = row?.upkeepId || ids.value;
  await proxy?.$modal.confirm("是否确认删除电梯保养信息编号为\"" + _upkeepIds + "\"的数据项？").finally(() => loading.value = false);
  await delUpkeepInfo(_upkeepIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download("iot/upkeepInfo/export", {
    ...queryParams.value
  }, `upkeepInfo_${new Date().getTime()}.xlsx`);
};

onMounted(() => {
  getList();
});
</script>
